﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Cit.CDE.Net.Service.ModuleFactory;
using Cit.CDE.Net.Module;
using System.Collections.Generic;
using Cit.CDE.Net.Module.Commons;
using cde.SystemCommon;
using Cit.CDE.Net.Service;
using System.Text;

using Module;
using Telerik.WebControls;
using System.IO;


/// <summary>
/// Copyright (C)   jinggubang
/// FileName:        CourseViews.aspx.cs           
/// Author :         wangbc
/// CDT:             2012-02-01           
/// Version:         1.0.0.0                
/// Depiction:       课件详细   
/// </summary>
public partial class main_admin_CourseViews : UserControl.CommonPage
{
    /// <summary>
    /// 课件EM
    /// </summary>
    private CDEEntityManager em;
    /// <summary>
    /// 课件Entity
    /// </summary>
    private CDEEntity m_entity;
    /// <summary>
    /// 课件Entity名字
    /// </summary>    
    private  string EM_FULL_NAME = WEBConstants.EM_CSW_MANAGE;

    /// <summary>
    /// 操作员ID
    /// </summary>
    private string opId;


    /// <summary>
    /// 管理员标识
    /// </summary>
    public string admin;

     //课件ID
    public string chkID = "";
    /// <summary>
    /// 页面初始化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {


        //管理员标识
        admin = (string)Request.QueryString.Get("admin");


        //当前登陆人信息
        LoginOprator op = (LoginOprator)Session[Cit.CDE.Net.Module.Commons.CDEConstants.SESSION_LOIN_OPRATOR];
        opId = op.SysID;
        

        //课件ID
        chkID = (string)Request.QueryString.Get("chkID");

        em = CDEEMFactory.getEMByFullName(EM_FULL_NAME);
        if (!Page.IsPostBack)
        {
            //ID不为空修改课件，否则新增课件
            if (chkID != "" && chkID != null)
            {
                //修改
                this.page_title.InnerText = "课件详细";
                CDESearchCriteria sc = new CDESearchCriteria(em);
                sc.addCondition("ID", chkID);
                List<CDEEntity> listEntity = CDEService.LoadEntities(em, sc);
                //课件ID是否存在
                if (listEntity.Count > 0)
                {
                    m_entity = listEntity[0];
                    m_entity.Operation = CDEDataOperation.UPDATE;

                    //文件名画面显示
                    string fileName = Util.objectToString(m_entity.get("FILE_PATH"));
                    if (fileName != "")
                    {
                        this.file_name.InnerHtml = fileName;
                        this.showFile.Style.Value = "display:block";
                    }

                    this.lbl_NAME_V.Text = Util.objectToString(m_entity.get("NAME"));
                    this.lbl_FOL_ID__FOL_NAME_V.Text = Util.objectToString(m_entity.get("FOL_ID__FOL_NAME")); 
                    this.lbl_CONTENT_TYPE__CATE_NAME_V.Text = Util.objectToString(m_entity.get("CONTENT_TYPE__CATE_NAME"));
                    this.lbl_SUBJECT_TYPE__CATE_NAME_V.Text = Util.objectToString(m_entity.get("SUBJECT_TYPE__CATE_NAME"));
                    this.lbl_CLASS_TYPE__CATE_NAME_V.Text = Util.objectToString(m_entity.get("CLASS_TYPE__CATE_NAME"));
                    this.lbl_ELECTIVE_TYPE__CATE_NAME_V.Text = Util.objectToString(m_entity.get("ELECTIVE_TYPE__CATE_NAME"));
                    this.lbl_BOOKS_TYPE__CATE_NAME_V.Text = Util.objectToString(m_entity.get("BOOKS_TYPE__CATE_NAME"));
                    this.lbl_VERSION_TYPE__CATE_NAME_V.Text = Util.objectToString(m_entity.get("VERSION_TYPE__CATE_NAME"));
                    this.lbl_OTHER_TYPE__CATE_NAME_V.Text = Util.objectToString(m_entity.get("OTHER_TYPE__CATE_NAME"));
                    //this.lbl_STATE_V.Text = Util.objectToString(m_entity.get("STATE"));
                    this.lbl_COUTENT_V.Text = Util.objectToString(m_entity.get("COUTENT"));

                    //查看次数
                    //当前时间
                    string Now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("-", "/");
                    string downLoads = Util.objectToNumbString(int.Parse(Util.objectToNumbString(m_entity.get("VIEWS"))) + 1);
                    m_entity.set("VIEWS", downLoads);
                    m_entity.set("UPDATE_USER", op.SysID);
                    m_entity.set("UPDATE_DATE", Now);
                    m_entity.Operation = CDEDataOperation.UPDATE;
                    //listEntity.Add(m_entity);
                    CDEService.UpdateEntities(em, listEntity);

                    

                } 
            }
            
            Session["MENTITY"] = m_entity;

            //课件信息设值到页面
            this.setControls(m_entity, Resources.ResourceErrMsg.ResourceManager);
            
        } 
    }

    /// <summary>
    /// 下载文件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void cmdDownload_Click(object sender, EventArgs e)
    {
        m_entity = (CDEEntity)Session["MENTITY"];
        
        //当前登陆人信息
        LoginOprator op = (LoginOprator)Session[Cit.CDE.Net.Module.Commons.CDEConstants.SESSION_LOIN_OPRATOR];
        //当前时间
        string Now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("-", "/");
        List<CDEEntity> listEntity = new List<CDEEntity>(); 

        string path = System.Configuration.ConfigurationSettings.AppSettings["Courseware"];
        string file = System.Web.HttpContext.Current.Server.MapPath(path + m_entity.get("CSW_ID").ToString().Trim());
        System.IO.FileInfo myfile = new System.IO.FileInfo(file);
        if (myfile.Exists)
        {

            //下载次数
            string downLoads = Util.objectToNumbString(int.Parse(Util.objectToNumbString(m_entity.get("DOWNLOADS"))) + 1);
            m_entity.set("DOWNLOADS", downLoads);
            m_entity.set("UPDATE_USER", op.SysID);
            m_entity.set("UPDATE_DATE", Now);
            m_entity.Operation = CDEDataOperation.UPDATE;
            listEntity.Add(m_entity);
            CDEService.UpdateEntities(em, listEntity);

            //读取文件
            System.IO.FileStream mystream = myfile.OpenRead();
            byte[] mybyte = new byte[myfile.Length];
            mystream.Read(mybyte, 0, Convert.ToInt32(mystream.Length));

            //下载
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(m_entity.get("FILE_PATH").ToString().Trim()));
            Response.AddHeader("Content-Length", mybyte.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.OutputStream.Write(mybyte, 0, mybyte.Length); //输出 
            Response.Flush();
            Response.End();
        }
        else
        {
            showMessageErrAndOk("文件未找到，请联系管理员!");
        }
    }


    /// <summary>
    /// 返回按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void cmdCancel_Click(object sender, EventArgs e)
    {
        string list_url = "CourseList.aspx";

        //管理员操作返回
        if (admin.Equals("1")||admin.Equals("4"))
        {
            EM_FULL_NAME = "WEB.CSW_MANAGE_ADMIN";
        }

        //课件审核
        if (admin.Equals("a"))
        {
            EM_FULL_NAME = "WEB.CSW_MANAGE_AUDIT";
            list_url = "CourseListA.aspx";      
        }
         
        if (admin.Equals("3")||admin.Equals("4")) {
            list_url = "CourseListV.aspx";        
        }

        if (Sel_MenuNode == null)
            return;
        StringBuilder url = new StringBuilder();
        url.Append("../course/" + list_url + "?EM=");
        url.Append(EM_FULL_NAME);
        url.Append("&sel_menu_id=");
        url.Append(Sel_MenuNode.Attributes["id"].Value);
        url.Append("&IsBackSc=1");
        Response.Redirect(url.ToString());

    } 
}
